package org.smartercontext.contextmger.utilities;

import java.util.ArrayList;
import java.util.List;
import org.smartercontext.contextmger.DTO.PaymentMethodDTO;
import org.smartercontext.contextmger.contextsphere.ContextManagerController;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

public class GetUserPaymentMethod {

	private List<PaymentMethodDTO>payments;
	
	public GetUserPaymentMethod(){
		
	}
	
	public void getUserPaymentMethods(Model usermodel, String username){
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		
		Resource resource = usermodel.getResource(nsUser+username);
		
		OntModel modelPrefs = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		modelPrefs.setNsPrefixes(ContextManagerController.getMappings());
		Property payMMT = modelPrefs.createProperty(ContextManagerController.getNsSchemaShopping()+"paymentMethodType");
		Property prefpayMMT = modelPrefs.createProperty(ContextManagerController.getNsSchemaShopping()+"preferredUserPaymentMethod");
		Property grName = modelPrefs.createProperty(ContextManagerController.getNsSchemaGR()+"name");
		Property grDescription = modelPrefs.createProperty(ContextManagerController.getNsSchemaGR()+"description");
		DatatypeProperty billingAddr = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"billingAddress");
		DatatypeProperty cardnumber = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"carNumber");
		DatatypeProperty expmonth = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"expirationMonth");
		DatatypeProperty expyear = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"expirationYear");
		DatatypeProperty nameoncard = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"nameOnCard");
		
		StmtIterator iterator = resource.listProperties(prefpayMMT);
		payments = new ArrayList<>();
		while(iterator.hasNext()){
			
			PaymentMethodDTO temp = new PaymentMethodDTO();
			
			Statement stmt = iterator.next();
			String object = stmt.getObject().toString();
			String method = object.split("#")[1];
			temp.setMethodType(method);
			
			Resource userPayM = usermodel.getResource(object);
			Statement stmt2=userPayM.getProperty(payMMT);
			String objetc2 = stmt2.getObject().toString();
			
			Resource grPaymentMethod = usermodel.getResource(objetc2);
			Statement stmt3 = grPaymentMethod.getProperty(grName);
			Statement stmt4 = grPaymentMethod.getProperty(grDescription);
			
			String name = stmt3.getObject().asLiteral().getString();
			String description = stmt4.getObject().asLiteral().getString();
			temp.setName(name);
			temp.setDescription(description);
			
			if(method.contains("American")||method.contains("Diners")||method.contains("Discover")
					||method.contains("JCB")||method.contains("Masterd")||method.contains("VISA")){
				
				Statement stmtBA = userPayM.getProperty(billingAddr);
				Statement stmtCN = userPayM.getProperty(cardnumber);
				Statement stmtEM = userPayM.getProperty(expmonth);
				Statement stmtEY = userPayM.getProperty(expyear);
				Statement stmtNOC = userPayM.getProperty(nameoncard);
				
				String billAddr = stmtBA.getObject().asLiteral().getString();
				String cardn = stmtCN.getObject().asLiteral().getString();
				int expm = stmtEM.getObject().asLiteral().getInt();
				int expy = stmtEY.getObject().asLiteral().getInt();
				String namoc = stmtNOC.getObject().asLiteral().getString();
				
				temp.setBillingAdddr(billAddr);
				temp.setCardnumber(cardn);
				temp.setExpirationMonth(expm);
				temp.setExpirationYear(expy);
				temp.setNameOnCard(namoc);
				
			}
			
			payments.add(temp);
		}
		
		
	}
	
	
	public Model addPaymentMethod(Model usermodel, String username, PaymentMethodDTO method){
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		OntModel modelPrefs = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		modelPrefs.setNsPrefixes(ContextManagerController.getMappings());
		Property payMMT = modelPrefs.createProperty(ContextManagerController.getNsSchemaShopping()+"paymentMethodType");
		Property prefpayMMT = modelPrefs.createProperty(ContextManagerController.getNsSchemaShopping()+"preferredUserPaymentMethod");
		Property grName = modelPrefs.createProperty(ContextManagerController.getNsSchemaGR()+"name");
		Property grDescription = modelPrefs.createProperty(ContextManagerController.getNsSchemaGR()+"description");
		DatatypeProperty billingAddr = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"billingAddress");
		DatatypeProperty cardnumber = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"carNumber");
		DatatypeProperty expmonth = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"expirationMonth");
		DatatypeProperty expyear = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"expirationYear");
		DatatypeProperty nameoncard = modelPrefs.createDatatypeProperty(ContextManagerController.getNsSchemaShopping()+"nameOnCard");
		Property rdfType = modelPrefs.createProperty(ContextManagerController.getRdf()+"type");
		Property pyMeth = modelPrefs.createProperty(ContextManagerController.getNsSchemaGR()+"Payment method");
		Property usPyMeth = modelPrefs.createProperty(ContextManagerController.getNsSchemaGR()+"UserPaymentMethod");
		
		Model userModel = deletePaymentMethod(usermodel, username, method);
		
		String name = method.getName();
		String descrip = method.getDescription();
		String methodP = method.getMethodType();
		
		Resource grPayment = modelPrefs.createResource(ContextManagerController.getNsSchemaGR()+methodP);
		userModel.add(grPayment,rdfType,pyMeth);
		
		Literal nameL = modelPrefs.createTypedLiteral(new String(name));
		Literal desL = modelPrefs.createTypedLiteral(new String(descrip));
		
		userModel.addLiteral(grPayment, grName, nameL);
		userModel.addLiteral(grPayment, grDescription, desL);
		
		Resource userPaymentM = modelPrefs.createResource(nsUser+methodP);
		userModel.add(userPaymentM,rdfType,usPyMeth);
		userModel.add(userPaymentM,payMMT,grPayment);
		
		if(methodP.contains("American")||methodP.contains("Diners")||methodP.contains("Discover")
				||methodP.contains("JCB")||methodP.contains("Masterd")||methodP.contains("VISA")){
			
			Literal billA = modelPrefs.createTypedLiteral(new String(method.getBillingAdddr()));
			Literal cardN = modelPrefs.createTypedLiteral(new String(method.getCardnumber()));
			Literal expM = modelPrefs.createTypedLiteral(method.getExpirationMonth());
			Literal expY = modelPrefs.createTypedLiteral(method.getExpirationYear());
			Literal nameC = modelPrefs.createTypedLiteral(new String(method.getNameOnCard()));
			
			userModel.addLiteral(userPaymentM, billingAddr, billA);
			userModel.addLiteral(userPaymentM, cardnumber, cardN);
			userModel.addLiteral(userPaymentM, expmonth, expM);
			userModel.addLiteral(userPaymentM, expyear, expY);
			userModel.addLiteral(userPaymentM, nameoncard, nameC);
				
			
		}
		
		Resource resource = userModel.getResource(nsUser+username);
		userModel.add(resource,prefpayMMT,userPaymentM);
		
		
		
		
		return userModel;
		
	}
	
	public Model deletePaymentMethod(Model usermodel, String username, PaymentMethodDTO method){
		
		String nsUser="http://smartercontext.org/vocabularies/rdf/"+username+".rdf#";
		OntModel modelPrefs = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		modelPrefs.setNsPrefixes(ContextManagerController.getMappings());
		Property payMMT = modelPrefs.createProperty(ContextManagerController.getNsSchemaShopping()+"paymentMethodType");
		Property prefpayMMT = modelPrefs.createProperty(ContextManagerController.getNsSchemaShopping()+"preferredUserPaymentMethod");
		
		Resource resource = usermodel.getResource(nsUser+username);
		
		if(resource.hasProperty(prefpayMMT)){
		
		Statement stmt = resource.getProperty(prefpayMMT);
		
		String object = stmt.getObject().toString();
		
		Resource userPaymentM = usermodel.getResource(object);
		Statement stmt1 = userPaymentM.getProperty(payMMT);
		String object1 = stmt1.getObject().toString();
		
		Resource grPaymentM = usermodel.getResource(object1);
		StmtIterator stmstGR = grPaymentM.listProperties();
		usermodel.remove(stmstGR);
		
		StmtIterator stmtsUPM = userPaymentM.listProperties();
		usermodel.remove(stmtsUPM);
		
		resource.removeAll(prefpayMMT);
		}
		
		return usermodel;
		
	}

	public List<PaymentMethodDTO> getPayments() {
		return payments;
	}

	public void setPayments(List<PaymentMethodDTO> payments) {
		this.payments = payments;
	}
	
	
	
}
